package com.ruoyi.car.controller;

import com.ruoyi.car.domain.CarInfo;
import com.ruoyi.car.service.ICarInfoService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 车辆车款Controller
 *
 * @author liujinlei
 * @date 2022-01-15
 */
@RestController
@RequestMapping("/info")
public class CarInfoController extends BaseController {
    @Autowired
    private ICarInfoService carInfoService;

    /**
     * 查询车辆车款列表
     */
    @RequiresPermissions("car:info:list")
    @GetMapping("/list")
    public TableDataInfo list(CarInfo carInfo) {
        startPage();
        List<CarInfo> list = carInfoService.selectCarInfoList(carInfo);
        return getDataTable(list);
    }

    /**
     * 导出车辆车款列表
     */
    @RequiresPermissions("car:info:export")
    @Log(title = "车辆车款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarInfo carInfo) {
        List<CarInfo> list = carInfoService.selectCarInfoList(carInfo);
        ExcelUtil<CarInfo> util = new ExcelUtil<CarInfo>(CarInfo.class);
        util.exportExcel(response, list, "车辆车款数据");
    }

    /**
     * 获取车辆车款详细信息
     */
    @RequiresPermissions("car:info:query")
    @GetMapping(value = "/{carId}")
    public AjaxResult getInfo(@PathVariable("carId") Long carId) {
        return AjaxResult.success(carInfoService.selectCarInfoByCarId(carId));
    }

    /**
     * 新增车辆车款
     */
    @RequiresPermissions("car:info:add")
    @Log(title = "车辆车款", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarInfo carInfo) {
        return toAjax(carInfoService.insertCarInfo(carInfo));
    }

    /**
     * 修改车辆车款
     */
    @RequiresPermissions("car:info:edit")
    @Log(title = "车辆车款", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarInfo carInfo) {
        return toAjax(carInfoService.updateCarInfo(carInfo));
    }

    /**
     * 删除车辆车款
     */
    @RequiresPermissions("car:info:remove")
    @Log(title = "车辆车款", businessType = BusinessType.DELETE)
    @DeleteMapping("/{carIds}")
    public AjaxResult remove(@PathVariable Long[] carIds) {
        return toAjax(carInfoService.deleteCarInfoByCarIds(carIds));
    }
}
